package SecondeStage.FanShe;

import java.lang.reflect.Field;

public class Class02 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        String classALLPath = "SecondeStage.FanShe.car";
        // 1.获取Car类 对应的 Class对象
        // <?>表示不确定的Java类型
        Class<?> cls = Class.forName(classALLPath);
        // 2.输出cls
        System.out.println(cls); // 显示cls对象是哪个类的class对象
        System.out.println(cls.getClass()); //输出运行类型 java.lang.Class
        // 3.得到包名
        System.out.println(cls.getPackage().getName());
        // 4.得到类名
        System.out.println(cls.getName());
        // 5.通过cls创建对象实例
        car car = (car) cls.newInstance();
        System.out.println(car);
        // 6.通过反射获得属性
        Field brand = cls.getField("brand");
        System.out.println(brand.get(car));
        // 7.通过反射给属性赋值
        brand.set(car,"奔驰");
        System.out.println(brand.get(car));
        // 8.希望得到所有的属性
        Field[] fields=cls.getFields();
        for (Field field :fields) {
            System.out.println(field.getName());
        }

    }
}

class car{
    public String brand="宝马";
    public int price=700000;
    public String color="白色";

    @Override
    public String toString() {
        return "car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                ", color='" + color + '\'' +
                '}';
    }
}
